package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录", notes = "员工登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);


    // 创建员工登录时生成的JWT令牌及其相关信息
    Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        // 构建员工登录响应对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                //用这个构建器需要加@Builder注解在类上
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "员工退出", notes = "员工退出接口")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    //也可以根据新增员工接口对应的DTO
    /*
    封装参数可以方便校验 在参数类的变量上添加注解可以换和方便的完成数据校验,而且代码可读性很高
    如果你用实体类来传参 不需要的变量是对性能的浪费 如果接口需要的参数很少并且不需要做数据校验的时候,也可以不做封装直接传参
     */
    @PostMapping
    @ApiOperation(value = "新增员工", notes = "新增员工接口")
     public Result add(@RequestBody EmployeeDTO employee){
        log.info("新增员工：{}", employee);
//        System.out.println("当前线程的id:"+Thread.currentThread().getId());客户端发起的每一次请求都是单独的线程
         employeeService.add(employee);
         return Result.success();

     }


    /**
     * 分页查询员工
     * @param employeePageQueryDTO
     * @return
     */
     @GetMapping("/page")
     @ApiOperation(value = "分页查询员工", notes = "分页查询员工接口")
     public Result<PageResult>page(EmployeePageQueryDTO employeePageQueryDTO){
         log.info("分页查询员工：{}", employeePageQueryDTO);
         PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
         return Result.success(pageResult);
     }


    /**
     * 修改员工状态
     * @param status
     * @param id
     * @return
     */
     @PostMapping("/status/{status}")
     @ApiOperation(value = "修改员工状态", notes = "修改员工状态接口")
     public Result startOrStop(@PathVariable Integer status,Long id){//id是地址栏传递过来的参数
         log.info("修改员工状态：{},{}", status,id);
         employeeService.startOrStop(status,id);
         return Result.success();
     }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
     @GetMapping("/{id}")
     @ApiOperation(value = "根据id查询员工", notes = "根据id查询员工接口")
     public Result<Employee> getById(@PathVariable Long id){
         Employee employee = employeeService.getById(id);
         return Result.success(employee);

     }


     @PutMapping
     @ApiOperation(value = "修改员工信息", notes = "修改员工信息接口")
     public Result update(@RequestBody EmployeeDTO employeeDTO) {
         log.info("修改员工信息：{}", employeeDTO);
         employeeService.update(employeeDTO);
         return Result.success();
     }

     @PutMapping("/editPassword")
     @ApiOperation(value = "修改员工密码", notes = "修改员工密码接口")
     public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
         log.info("修改员工密码：{}", passwordEditDTO);
         employeeService.editPassword(passwordEditDTO);
         return Result.success("密码修改成功");
     }

}
